using System;
using UnityEngine;

public class Music : MonoBehaviour
{
	[Serializable]
	public class MusicLand
	{
		public AudioClip[] audioClip;
	}

	[SerializeField]
	private AudioSource audioSource;

	[SerializeField]
	private AudioClip menuMusic;

	[SerializeField]
	private AudioClip[] musics;

	[SerializeField]
	private bool[] playedMusic;

	private int amountOfPlayedSongs;

	private void Awake()
	{
		if (!Library.music)
		{
			Library.music = this;
		}
		RestartSongsRegister();
		UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
	}

	private void RestartSongsRegister()
	{
		playedMusic = new bool[musics.Length];
		amountOfPlayedSongs = 0;
	}

	public void Play(SceneName sceneName)
	{
		AudioClip audioClip = GetAudioClip(base.name);
		switch (sceneName)
		{
		case SceneName.Menu:
		case SceneName.BodyShapeEditor:
		case SceneName.CreatureEditor:
			audioClip = menuMusic;
			break;
		case SceneName.Game:
		{
			int num;
			do
			{
				num = UnityEngine.Random.Range(0, musics.Length);
			}
			while (playedMusic[num]);
			playedMusic[num] = true;
			audioClip = musics[num];
			if (++amountOfPlayedSongs == musics.Length)
			{
				RestartSongsRegister();
			}
			break;
		}
		}
		if (!audioSource.isPlaying || !(audioSource.clip == audioClip))
		{
			audioSource.clip = audioClip;
			audioSource.Play();
		}
	}

	public void Stop()
	{
		audioSource.Stop();
	}

	private AudioClip GetAudioClip(string name)
	{
		AudioClip[] array = musics;
		foreach (AudioClip audioClip in array)
		{
			if (audioClip.name == name)
			{
				return audioClip;
			}
		}
		return null;
	}
}
